/**
 * Filename: ContactGroupUserAdapter.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/9 1:22
 */
package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNContactUser;
import com.github.postsanf.yinian.constants.CommonConstants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 联系人分组列表
 */
public class ContactGroupUserAdapter2 extends BaseAdapter implements SectionIndexer {
    private List<YNContactUser> lists;
    private Context context;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;
    private Resources mResource;
    private SparseIntArray positionOfSection;
    private SparseIntArray sectionOfPosition;

    public ContactGroupUserAdapter2(List<YNContactUser> lists, Context context) {
        this.lists = lists;
        this.context = context;
        isSelected = new HashMap<Integer, Boolean>();
        initDate();
    }

    public void setResource(Resources mResource) {
        this.mResource = mResource;
    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < lists.size(); i++) {
            getIsSelected().put(i, false);
        }
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        ContactGroupUserAdapter2.isSelected = isSelected;
    }

    //更新列表
    public void updateListView(List<YNContactUser> lists) {
        this.lists = lists;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public YNContactUser getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final YNContactUser mContent = lists.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.yn_item_aitecontact, null);
            holder.item_cb = (CheckBox) convertView.findViewById(R.id.item_cb);
            holder.catalog = (TextView) convertView.findViewById(R.id.contact_catalog);
            holder.contactimg = (ImageView) convertView.findViewById(R.id.contactimg);
            holder.contactname = (TextView) convertView.findViewById(R.id.contactname);
//            holder.contactnumber = (TextView) convertView.findViewById(R.id.contactnumber);
            holder.setposition = (TextView) convertView.findViewById(R.id.setposition);
            if (mResource != null) {
                holder.item_cb.setButtonDrawable(mResource.getDrawable(mResource.getIdentifier("radio_sel", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
            }
            holder.contactname.setText(lists.get(position).getName());
//            holder.contactnumber.setText(lists.get(position).getNumber());
            holder.setposition.setText(lists.get(position).getId() + "");
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.contactname.setText(lists.get(position).getName());
//            holder.contactnumber.setText(lists.get(position).getNumber());
            holder.setposition.setText(lists.get(position).getId() + "");
        }
        // 根据position获取分类的首字母的Char ascii值
//        int section = getSectionForPosition(position);
        // 判断是否第一次出现,若是则显示分类
//        if (position == getPositionForSection(section)) {
//            holder.catalog.setVisibility(View.VISIBLE);
//            holder.catalog.setText(mContent.getNameLetters());
//        } else {
//            holder.catalog.setVisibility(View.GONE);
//        }
        String header = mContent.getNameLetters();
        if (position == 0 || header != null
                && !header.equals(getItem(position - 1).getNameLetters())) {
            if ("".equals(header)) {
                holder.catalog.setVisibility(View.GONE);
            } else {
                holder.catalog.setVisibility(View.VISIBLE);
                holder.catalog.setText(header);
            }
        } else {
            holder.catalog.setVisibility(View.GONE);
        }
        // 根据isSelected来设置checkbox的选中状况
        holder.item_cb.setChecked(getIsSelected().get(position));
        holder.item_cb.setChecked(getIsSelected().get(lists.get(position).getId()));
        return convertView;
    }

    public int getPositionForSection(int section) {
        return positionOfSection.get(section);
    }

    public int getSectionForPosition(int position) {
        return sectionOfPosition.get(position);
    }

    List<String> list;

    @Override
    public Object[] getSections() {
        positionOfSection = new SparseIntArray();
        sectionOfPosition = new SparseIntArray();
        int count = getCount();
        list = new ArrayList<String>();
        list.add(context.getString(R.string.search_header));
        positionOfSection.put(0, 0);
        sectionOfPosition.put(0, 0);
        for (int i = 1; i < count; i++) {
            String letter = getItem(i).getNameLetters();
            int section = list.size() - 1;
            if (list.get(section) != null && !list.get(section).equals(letter)) {
                list.add(letter);
                section++;
                positionOfSection.put(section, i);
            }
            sectionOfPosition.put(i, section);
        }
        return list.toArray(new String[list.size()]);
    }


    // 提取英文的首字母，非英文字母用#代替。
    private String getAlpha(String str) {
        String sortStr = str.trim().substring(0, 1).toUpperCase();
        // 正则表达式，判断首字母是否是英文字母
        if (sortStr.matches("[A-Z]")) {
            return sortStr;
        } else {
            return "#";
        }
    }

    public static class ViewHolder {
        public CheckBox item_cb;
        public TextView catalog;
        public ImageView contactimg;
        public TextView contactname;
        public TextView contactnumber;
        public TextView setposition;
    }
}
